package com.jinyuan.view.circlemenu;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;

public class CircleAnimations {

	// 用来适配不同的分辨率
	public static int xOffset = 0;
	public static int yOffset = 0;
	public static int radius_dip = 80;
	public static float radius = 0f;
	public static double sector = Math.PI;

	public static void initOffset(Context context) {
		// -- 设置偏移量
		// xOffset = (int) (10.667 *
		// context.getResources().getDisplayMetrics().density);
		// yOffset = -(int) (8.667 *
		// context.getResources().getDisplayMetrics().density);
		radius = CircleAnimations.dip2px(context, radius_dip);
		sector = Math.PI;
	}

	// 加号的动画
	public static Animation getRotateAnimation(float fromDegrees,
			float toDegrees, int durationMillis) {
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotate.setDuration(durationMillis);
		rotate.setFillAfter(true);
		return rotate;
	}

	// 图标的动画(隐藏动画)
	public static void startAnimationsIn(final ViewGroup viewgroup,
			int durationMillis) {
//		System.out.println("-----图标入动画-----");
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
			final Button inoutimagebutton = (Button) viewgroup.getChildAt(i);
			// -- 测试代码
			Animation animation = new TranslateAnimation(0F,
					(float) Math.cos((17 - 2 * (i + 1)) * Math.PI / 8) * radius, 0F,
					-(float) (Math.sin((17 - 2 * (i + 1)) * Math.PI / 8) * radius));
			animation.setFillAfter(false);
			animation.setDuration(durationMillis);
			animation.setStartOffset((i * 100)
					/ (-1 + viewgroup.getChildCount()));// 下一个动画的偏移时间
			animation.setAnimationListener(new Animation.AnimationListener() {
				@Override
				public void onAnimationStart(Animation arg0) {
				}

				@Override
				public void onAnimationRepeat(Animation arg0) {
				}

				@Override
				public void onAnimationEnd(Animation arg0) {
					inoutimagebutton.setVisibility(View.GONE);
					inoutimagebutton.setClickable(false);
					inoutimagebutton.setFocusable(false);
					viewgroup.setVisibility(View.GONE);
				}
			});
			inoutimagebutton.startAnimation(animation);
		}
	}

	// 图标的动画(展现动画)
	public static void startAnimationsOut(ViewGroup viewgroup,
			int durationMillis) {
//		System.out.println("-----图标出动画-----");
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
			Button inoutimagebutton = (Button) viewgroup.getChildAt(i);
			viewgroup.setVisibility(View.VISIBLE);
			inoutimagebutton.setVisibility(View.VISIBLE);
			inoutimagebutton.setClickable(true);
			inoutimagebutton.setFocusable(true);
			// -- 动画
			float fromX = (float) (Math.cos((17 - 2 * (i + 1)) * Math.PI / 8)
					* radius);
			float fromY = -(float) (Math.sin((17 - 2 * (i + 1)) * Math.PI / 8) * radius);
			Animation animation = new TranslateAnimation(fromX, 0F, fromY, 0F);
			animation.setFillAfter(false);
			animation.setDuration(durationMillis);
			animation.setStartOffset(((viewgroup.getChildCount() - i) * 100)
					/ (-1 + viewgroup.getChildCount()));// 下一个动画的偏移时间
			animation.setInterpolator(new OvershootInterpolator(2F));// 动画的效果
																		// 弹出再回来的效果
			inoutimagebutton.startAnimation(animation);
		}
	}

	// icon缩小消失的动画
	public static Animation getMiniAnimation(int durationMillis) {
		Animation miniAnimation = new ScaleAnimation(1.0f, 0f, 1.0f, 0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		miniAnimation.setDuration(durationMillis);
		miniAnimation.setFillAfter(true);
		return miniAnimation;
	}

	// icon放大渐变消失的动画
	public static Animation getMaxAnimation(int durationMillis) {
		AnimationSet animationset = new AnimationSet(true);

		Animation maxAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		Animation alphaAnimation = new AlphaAnimation(1, 0);

		animationset.addAnimation(maxAnimation);
		animationset.addAnimation(alphaAnimation);

		animationset.setDuration(durationMillis);
		animationset.setFillAfter(true);
		return animationset;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static float dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (dpValue * scale + 0.5f);
	}
	
	// 背景的动画(入动画)
	public static void startBackgroudAnimationsOut(View view, int durationMillis) {
		view.setVisibility(View.VISIBLE);
		Animation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animation.setFillAfter(false);
		animation.setDuration(durationMillis);
		view.startAnimation(animation);
	}

	// 背景的动画(出动画)
	public static void startBackgroudAnimationsIn(final View view,
			int durationMillis) {
		Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animation.setFillAfter(false);
		animation.setDuration(durationMillis);
		animation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation arg0) {}
			@Override
			public void onAnimationRepeat(Animation arg0) {}
			@Override
			public void onAnimationEnd(Animation arg0) {
				view.setVisibility(View.GONE);
			}
		});
		view.startAnimation(animation);
	}

}